package com.fresh.bus.chat.business;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.fresh.bus.chat.domain.TreeLog;
import com.fresh.bus.chat.domain.UserLog;
import com.fresh.bus.chat.util.DateUtil;

@Component
public class UserLogService extends BaseLogService {
	private static final Logger log = LoggerFactory
			.getLogger(UserLogService.class);
	private StringRedisTemplate template;

	@Autowired
	public UserLogService(StringRedisTemplate template) {
		this.template = template;
	}

	public String updateLog(String owner, String from, int actionType, String msgKey) {
		String uid = KeyUtils.uid(owner);
		BoundHashOperations<String, String, String> ops = template
				.boundHashOps(uid);
		String userLog = ops.get("userLog");
		if(userLog==null || userLog.length()<=0){
			userLog = "[]";
		}else{
			userLog = appendLog(userLog, from, actionType, msgKey);
		}
		ops.put("userLog", userLog);
		return userLog;
	}
	
	protected String appendLog(String srcLog, String who,int actionType, String msgKey) {
		UserLog log = new UserLog();
		String now = DateUtil.convertDateToString(new Date());
		log.setD(now);
		log.setU(who);
		log.setA(actionType);
		log.setT(msgKey);
		srcLog = appendLog(srcLog, log);
		return srcLog;
	}	
}
